package com.lmh.controller;

import com.lmh.pojo.*;
import com.lmh.service.LoginService;
import com.sun.deploy.net.HttpResponse;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
/*import java.time.temporal.TemporalQueries;*/
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("login")

    public String login(String email, String password, Model m, HttpSession session){

        if(email !=null && password !=null && email !="" && password !="" ){
            /*System.out.println("ssssssss");*/
            System.out.println("aaaaaaaa");
            /*System.out.println(password);*/
            User user=loginService.login(email,password);
            /*System.out.println(user);*/
            if(user !=null && user.getEmail().equals(email) && user.getPassword().equals(password)){
                String s=null;
                try {
                    HtmlEmail email1 =new HtmlEmail();
                    email1.setHostName("smtp.163.com");
                    email1.setCharset("utf-8");
                    email1.addTo(email);
                    email1.setFrom("bfc20179445@163.com","cs");
                    email1.setAuthentication("bfc20179445@163.com","YBGDPCUESZIPNDDF");
                    email1.setSubject("登录验证码");
                    Random rd=new Random();
                    int uid=rd.nextInt(10000);
                    /*System.out.println(uid);*/
                    s=uid+"";
                    email1.setMsg(s);
                    email1.send();

                } catch (EmailException e) {
                    e.printStackTrace();

                }
                session.setAttribute("s",s);
                session.setAttribute("user",user);
                return "yzm";

            }else if(email.contains("@") && (email.endsWith("com") ||email.endsWith("cn"))){
                m.addAttribute("msg","邮箱格式不正确");
                return "login";
            }else {
                m.addAttribute("msg","用户名或密码不正确");
                return "login";
            }
        }else{
            m.addAttribute("msg","请输入用户名和密码");
            return "login";
        }

    }


    @RequestMapping("/yzm")
    public String yzm(String yzm,HttpSession session,Model m){
        String s=(String) session.getAttribute("s");
        User user=(User) session.getAttribute("user");
        if(yzm.equals(s)){
            System.out.println(user.getUserId());
            Userinfo userinfo=loginService.infoall(user.getUserId());
            System.out.println(userinfo);
            session.setAttribute("userinfo",userinfo);
            return "show";
        }else {
            m.addAttribute("验证码不正确");
            return "login";
        }

    }



    @RequestMapping("/registered")
    public String registered(String userName, String email, String password, String password1, Model m){
        /*System.out.println("密码"+password);
        System.out.println("确认密码"+password1);
        System.out.println(email);
        System.out.println(email.contains("@"));
        System.out.println(email.substring(email.lastIndexOf(".")));*/
        if(userName !=null && email !=null && password !=null && password1 !=null && userName !="" && email !="" && password !="" && password1 !=""){
            if(password.equals(password1) && (email.endsWith("com") ||email.endsWith("cn")) && email.length()<64 && password.length()<32 && email.contains("@")){
                String ui=UUID.randomUUID().toString();
                String userId=ui.replace("-","");
                List<Userinfo> list =loginService.edan(email,userName);
                System.out.println(list);
                int count=0;
                for (Userinfo u:list){
                    if(u.getEmail() !=null || u.getUserName() !=null){
                        count++;
                    }
                }
                if(count ==0){
                    int i=loginService.adduser(userName,userId,email,password);
                    if(i >0){
                        return "login";
                    }else {
                        return "";
                    }
                }else{
                    m.addAttribute("mag","邮箱已被注册");
                    return "registered";
                }

            }else if(password.equals(password) ==false){
                m.addAttribute("mag","密码不一致");
                return "registered";
            }else if(email.contains("@") ==false && (email.endsWith("com") ||email.endsWith("cn"))){
                m.addAttribute("mag","邮箱格式不正确");
                return "registered";
            }else {
                m.addAttribute("mag","邮箱或者密码过长");
                return "registered";

            }
        }else {
            m.addAttribute("mag","输入框不能为空");
            return "registered";
        }



    }
    @RequestMapping("/mmm")
    public String mmm(){
        return "login";
    }

    @RequestMapping("/updatedanInfo")
    public String updatedanInfo(){
        return "updateinfo";
    }


    @RequestMapping("/updatedanInfo1")
    @ResponseBody
    public List<Provines> updatedanInfo1( Model m){

        List<Provines> data=loginService.address();
        System.out.println("省："+data);

            return data;

    }


    @RequestMapping("/citiesquan")
    @ResponseBody
    public List<Cities> citiesquan(@RequestBody CitiesVo citiesVo){
        System.out.println("传值："+citiesVo.getProvinceid());
        List<Cities> cities=loginService.citiesquan(citiesVo.getProvinceid());
        System.out.println(cities);
        return cities;


    }



    @RequestMapping("/arasquan")
    @ResponseBody
    public List<Areas> arasquan(@RequestBody AreasVo areasVo){
        System.out.println(areasVo.getCityid());
        List<Areas> areas=loginService.arasquan(areasVo.getCityid());
        System.out.println(areas);
        return areas;
    }

    @RequestMapping("/updateinfo")
    public String updateinfo(Userinfo userinfo){
        System.out.println(userinfo);
        int i=loginService.updateinfo(userinfo);
        if(i !=0){
            return "show";
        }else{
            return "";
        }

    }


    /**
     *
     * @param password
     * @param newpassword
     * @param qpassword
     * @param session
     * @param m
     * @return
     */
    @RequestMapping("/updatepw")
    public String updatepw(String password,String newpassword,String qpassword,HttpSession session,Model m){
        System.out.println("password:"+password);
        System.out.println("newpassword:"+newpassword);
        System.out.println("qpassword:"+qpassword);
        Userinfo userinfo=(Userinfo) session.getAttribute("userinfo");
        System.out.println("userinfo:"+userinfo);
        String p=userinfo.getU().getPassword();
        System.out.println("p:"+p);
        if( password.equals(p) && password !=newpassword && newpassword.equals(qpassword)){
            User user=new User();
            System.out.println("id:"+userinfo.getUserId());
            user.setUserId(userinfo.getUserId());
            user.setPassword(newpassword);
            int i=loginService.updatepw(user);
            if(i !=0){
                return "show";
            }else {
                m.addAttribute("msg","密码修改失败");
                return "updatepassword";
            }
        }else if(password.equals(p)==false){
            m.addAttribute("msg","密码不正确");
            return "updatepassword";
        }else if(password.equals(newpassword)==true){
            m.addAttribute("msg","新密码和旧密码重复");
            return "updatepassword";
        }else {
            m.addAttribute("msg","新密码和确认密码不一致");
            return "updatepassword";
        }

    }

    @RequestMapping("/anquan")
    public String anquan(){
        return "updatepassword";
    }

}
